<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let p1 = new Promise((resolve, reject) => {
      try {
        console.log("请求A的数据......");
        // throw new Error("A错误")
        setTimeout(() => {
          console.log("请求A的成功");
          resolve("A成功")
        }, 1500)
      } catch (e) {
        reject(e.message)
      }
    })

    let p2 = new Promise((resolve, reject) => {
      try {
        console.log("请求B的数据......");
        // throw new Error("B错误")
        setTimeout(() => {
          console.log("请求B的成功");
          resolve("B成功")
        }, 2500)
      } catch (e) {
        reject(e.message)
      }
    })

    let p3 = new Promise((resolve, reject) => {
      try {
        console.log("请求C的数据......");
        // throw new Error("C错误")
        setTimeout(() => {
          console.log("请求C的成功");
          resolve("C成功")
        }, 3500)
      } catch (e) {
        reject(e.message)
      }
    })



    // promise.race方法：
    // — 接收一个数组(iterator类型)作为参数
    // — 默认状态下 pending状态，值为undefined 
    // — 接收的是状态最先发生改变的那个promise实例(无论成功还是失败),返回值与他保持一致
    const result = Promise.race([p1, p2, p3])
    console.log("result", result);
  </script>
</body>

</html>